package com.zjh.photoshopstudy.app.video;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.zjh.photoshopstudy.Model.AppUserEntity;
import com.zjh.photoshopstudy.Model.CommentEntity;
import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.Utils.CommentImageClick;
import com.zjh.photoshopstudy.Utils.Data;
import com.zjh.photoshopstudy.Utils.SharePreferencesUtils;

import java.text.DateFormat;
import java.util.List;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentView> {

    private List<CommentEntity> vCommentEntityList;
    private Context context;
    private OnCommentClickListener commentClickListener;
    private AppUserEntity userEntity;
    private VideoEntity videoEntity;

    public CommentAdapter(List<CommentEntity> vCommentEntityList, VideoEntity videoEntity, Context context, OnCommentClickListener commentClickListener) {
        this.vCommentEntityList = vCommentEntityList;
        this.context = context;
        this.commentClickListener = commentClickListener;
        userEntity = SharePreferencesUtils.getLoginUser(context);
        this.videoEntity = videoEntity;
    }

    public void setvCommentEntityList(List<CommentEntity> vCommentEntityList) {
        this.vCommentEntityList = vCommentEntityList;
    }

    public List<CommentEntity> getvCommentEntityList() {
        return vCommentEntityList;
    }

    @NonNull
    @Override
    public CommentView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CommentView(LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CommentView holder, final int position) {
        final CommentEntity entity = vCommentEntityList.get(position);
        Glide.with(context).load(Data.webRoot_URL + entity.getAppUserEntity().getuIcon()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.mImage);
        holder.mName.setText(entity.getAppUserEntity().getuName());
        holder.mContent.setMovementMethod(LinkMovementMethod.getInstance());
        if (userEntity != null && (userEntity.getId() == entity.getAppUserEntity().getId() || userEntity.getId() == videoEntity.getvAppUserEntity().getId())) {
            holder.delete.setVisibility(View.VISIBLE);
        }else {
            holder.delete.setVisibility(View.INVISIBLE);
        }
        if (entity.getcParentId() == null || entity.getcParentId() != 1) {
            holder.mContent.setText(getSpannableString(entity, false));
        } else {
            holder.mContent.setText(getSpannableString(entity, true));
        }

        holder.mContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commentClickListener.onClick(entity);
            }
        });

        holder.mTime.setText(DateFormat.getInstance().format(entity.getCreateTime()));

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VideoHttp http = new VideoHttp();
                http.deleteComment(new OnCommentDelete(), entity, position);
            }
        });
    }

    private SpannableStringBuilder getSpannableString(CommentEntity entity, boolean isReply) {
        SpannableStringBuilder spannable = new SpannableStringBuilder();
        if (isReply) {
            spannable.append("回复 " + entity.getUserName() + " ：");
        }
        String str = entity.getcContent();
        String s = "<i>";
        String e = "</i>";
        while (str.contains(s) && str.contains(e)) {
            String item = str.substring(str.indexOf(s), str.indexOf(e));
            String url = item.substring(item.indexOf(">") + 1);
            spannable.append(str.substring(0, str.indexOf(s)));
            int start = spannable.length();
            spannable.append("查看图片 ");
            CommentImageClick clickableSpan = new CommentImageClick(context, "upload/comment/" + url);
            int end = start + 4;
            spannable.setSpan(clickableSpan, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#FF00B8D4"));
            spannable.setSpan(colorSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            str = str.substring(str.indexOf(e) + 4);
        }
        spannable.append(str);
        return spannable;
    }

    @Override
    public int getItemCount() {

        return vCommentEntityList == null ? 0 : vCommentEntityList.size();
    }

    public class CommentView extends RecyclerView.ViewHolder {
        ImageView mImage;
        TextView mName;
        TextView mContent;
        TextView mTime;
        TextView delete;


        public CommentView(@NonNull View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.comment_name);
            mContent = itemView.findViewById(R.id.resume);
            mImage = itemView.findViewById(R.id.comment_uicon);
            mTime = itemView.findViewById(R.id.time);
            delete = itemView.findViewById(R.id.delete_btn);
        }
    }

    class OnCommentDelete {
        void onDeleteSuccess(int position) {
            vCommentEntityList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, vCommentEntityList.size() - position);
            Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
        }

        void onDeleteFail() {
            Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
        }
    }
}
